﻿package com.kuanter.kuanterauto.activity;

import java.util.List;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.kuanter.kuanterauto.R;
import com.kuanter.kuanterauto.adapter.MyMessageAdapter;
import com.kuanter.kuanterauto.baseui.ProApplication;
import com.kuanter.kuanterauto.dataservices.PushServices;
import com.kuanter.kuanterauto.db.DBManager;
import com.kuanter.kuanterauto.entity.MessageInfo;
import com.kuanter.kuanterauto.main.HomeActivityGroup;
import com.kuanter.kuanterauto.utils.DataLoader;
import com.umeng.analytics.MobclickAgent;

public class MyMessageActivity extends WuyangBaseActivity {
	private TextView include_image_2tv_arrow_TVtitle;
	private ListView my_message_lv;
	private DataLoader dataLoader;
	private ProApplication myApplication;
	private List<MessageInfo> messageList;
	private DBManager dBManager;
	
/*Handler handler = new Handler() {
		

		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpTask.RESULT_OK:
				
//				String result = msg.getData().getString("result");
				String result = (String) msg.obj;
				if (!result.equals("网络连接错误")) {
					try {
						if (StringUtil.isSuccess(result)) {// 判断返回数据中 status
							//listview 
							my_message_lv = (ListView)MyMessageActivity.this.findViewById(R.id.my_message_lv);
							JSONObject jsonObject = JSON.parseObject(result);
							List<MessageInfo> messageInfos = JSON.parseArray(jsonObject.getString("messageList"), MessageInfo.class);
							if(!(null == messageInfos)){
								my_message_lv.setAdapter(new MyMessageAdapter(messageInfos, MyMessageActivity.this));	
							}else {
								showToast("内容为空");
							}

						} else {
							finishProgress();
							// 显示返回数据中 errorMsg 字段值
							showToast(StringUtil.geterrorMsg(result));
						}
					} catch (JSONException e) {
						finishProgress();
						showToast("登录失败");
						e.printStackTrace();
					}
				} else {
					finishProgress();
					showToast("登录失败,网络f错误!");
				}
				break;
			case HttpTask.RESULT_ERROR:
				finishProgress();
				showToast("网络错误!");
				break;
			}
		}
	};*/

	private NotificationManager notificationManager;
	public void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	public void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.my_message);
		super.onCreate(savedInstanceState);
		//清空通知栏
		notificationManager=(NotificationManager)this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		notificationManager.cancel(0);
		
		IntentFilter filter = new IntentFilter();
		List<MessageInfo> l_m = PushServices.getmessageInfos();
		if(l_m != null) {
			l_m.clear();
			l_m = null;
		}
		PushServices.unReadMessage = 0;
		
		filter.addAction("MESSAGE_DELETE");
		//注册删除message消息广播
		registerReceiver(new MessageDeleteBroadcastReceiver(), filter );
		Intent intent = new Intent(HomeActivityGroup.show_action);
		intent.putExtra("status", HomeActivityGroup.disapper);
		sendBroadcast(intent);

		this.setHeaderControlViewText();
		this.initBodyControlView();
		this.registerListener();
		this.setBodyControlViewText();
		//dataLoader = new DataLoader(this);
		//List<NameValuePair> params = new ArrayList<NameValuePair>();
		//myApplication = (ProApplication)getApplication();
		//params.add(new BasicNameValuePair("deviceToken", myApplication.deviceToken));
		//dataLoader.DisplayData(HttpConfigAPI.SERVER_URL+HttpConfigAPI.QUERY_MESSAGE, params , handler, "", false); 
		my_message_lv = (ListView)MyMessageActivity.this.findViewById(R.id.my_message_lv);
		dBManager = new DBManager(this);
		messageList = dBManager.query();
		dBManager.closeDB();
		my_message_lv.setAdapter(new MyMessageAdapter(messageList, MyMessageActivity.this));	
		my_message_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MessageInfo messageInfo = messageList.get(position);
				Intent intent = new Intent(MyMessageActivity.this,MyMessageDetailActivity.class);
				intent.putExtra("position", position+"");
				intent.putExtra("sqlID", messageInfo.getSqlID());
				intent.putExtra("title", messageInfo.getTitle());
				intent.putExtra("pushTime", messageInfo.getPushTime());
				intent.putExtra("content", messageInfo.getContent());
				startActivity(intent);
			}
			
		});
	}
	@Override
	protected void setHeaderControlViewText() {
		// TODO Auto-generated method stub
		super.setHeaderTitleText(super.headerTitle, "我的消息");
		super.setHeaderLeftText(super.headerLeft, "返回");
		super.setHeaderRightText(super.headerRight, null);
	}

	@Override
	protected void initBodyControlView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void setBodyControlViewText() {
		// TODO Auto-generated method stub
	}

	@Override
	protected void registerListener() {
		// TODO Auto-generated method stub
		super.headerLeft.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.header_left:
			this.finish();
			break;

		default:
			break;
		}
	}
	@Override
	protected void getControlViewText() {
		// TODO Auto-generated method stub
		
	}
	
	private class MessageDeleteBroadcastReceiver extends BroadcastReceiver{

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			MyMessageAdapter messageAdapter = (MyMessageAdapter)my_message_lv.getAdapter();
			messageAdapter.deleteData(messageList.get(Integer.parseInt(intent.getStringExtra("position"))));
		}
		
	}

}
